package com.hanhai.zrb.api.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.api.biz.dao.mybatis.CityInfoMapper;
import com.hanhai.zrb.api.biz.service.CityInfoService;
import com.hanhai.zrb.model.project.CityInfo;
import com.hanhai.zrb.model.project.dto.CityInfoDto;

@Service("cityInfoService")
public class CityInfoServiceImpl implements CityInfoService{

	@Resource
	private CityInfoMapper cityInfoMapper;
	
	
	@Override
	public CityInfo selectByPrimaryKey(Integer id) {
		return cityInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public CityInfo selectByPostcode(String postcode) {
		return cityInfoMapper.selectByPostcode(postcode);
	}
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return cityInfoMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(CityInfo record) {
		return cityInfoMapper.insert(record);
	}

	@Override
	public int insertSelective(CityInfo record) {
		return cityInfoMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(CityInfo record) {
		return cityInfoMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(CityInfo record) {
		return cityInfoMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<CityInfo> findAllProvince() {
		return cityInfoMapper.findAllProvince();
	}

	@Override
	public List<CityInfo> findCityProvince(String postcode) {
		
		return cityInfoMapper.findCityProvince(postcode);
	}

	@Override
	public List<CityInfo> findCountryProvince(String postcode,String topCity) {
		
		return cityInfoMapper.findCountryProvince(postcode, topCity);
	}
	
	@Override
	public List<CityInfo> findChildCity(Integer topId) {
		return cityInfoMapper.findChildCity(topId);
	}

	@Override
	public CityInfo findByCityName(String cityName) {
		return cityInfoMapper.findByCityName(cityName);
	}

	@Override
	public CityInfo findByCityNameAndParentId(String cityName, Integer topId) {
		return cityInfoMapper.findByCityNameAndParentId(cityName, topId);
	}
	
	@Override
	public CityInfo findByParentName(String proName, String cityName,String countryName) {
		CityInfo province = findByCityName(proName);
		if(null!=cityName){
			CityInfo city = this.findByCityNameAndParentId(cityName, province.getId());
			return findByCityNameAndParentId(countryName, city.getId());
		}
		return findByCityNameAndParentId(countryName, province.getId());
	}

	@Override
	public List<CityInfo> findByCityAndParentName(String keyWord) {
		return cityInfoMapper.findByCityAndParentName(keyWord);
	}

	@Override
	public int countCityInfo(CityInfoDto dto) {
		return cityInfoMapper.countCityInfo(dto);
	}

	@Override
	public List<CityInfo> list(CityInfoDto dto) {
		return cityInfoMapper.list(dto);
	}
	
	
	@Override
	public List<CityInfo> selectByListPostCode(List<String> list) {
		return cityInfoMapper.selectByListPostCode(list);
	}
	
	@Override
	public List<CityInfo> selectAll() {
		return cityInfoMapper.selectAll();
	}
	
	@Override
	public Map<String, CityInfo> selectAllMap() {
		Map<String,CityInfo> map = new HashMap<String,CityInfo>();
		List<CityInfo> list = cityInfoMapper.selectAll();
		if(CollectionUtils.isNotEmpty(list)){
			for(CityInfo cityInfo:list){
				map.put(cityInfo.getPostcode(), cityInfo);
			}
		}
		return map;
	}
}
